<?php

class config {

    private $db = NULL;

    public function __construct(&$db = NULL) {
        if (!class_exists("db"))
            die("No DB class exists!");
        // DB already instaced
        if ($db != NULL)
            $this->db = $db;
    }

    /*
     * Load config and instance DB
     */

    public function load_config($override_path = "") {
        // Config check/parse
        if (!file_exists($override_path . "config"))
            die("Config is missing, use web portal to create initial config.\n");
        $config = parse_ini_file($override_path . "config", true);
        if (!$config)
            die("Cannot open config file, check permissions if under linux!");
        if ($this->db == NULL)
            $this->db = new db($config);
        $rows = $this->db->get_rows("settings");
        foreach ($rows as $row) {
//            if (DEBUG_SCRIPT)
//                print "DB: [{$row['section']}][{$row['name']}] = [{$row['value']}]\n";
            $config[$row['section']][$row['name']] = $row['value'];
        }
        // convert list to array
        $config['Processing']['allowed_formats'] = explode(",", $config['Processing']['allowed_formats']);
        $config['Processing']['unwanted_content'] = explode(",", $config['Processing']['unwanted_content']);
        $config['Processing']['unwanted_tags'] = explode(",", $config['Processing']['unwanted_tags']);
        $config['Subtitle']['language'] = explode(",", $config['Subtitle']['language']);
        $config['Series']['follow'] = explode(",", $config['Series']['follow']);
        return $config;
    }

    /*
     * Return instance of DB
     */

    public function get_db() {
        return $this->db;
    }

}

?>
